package com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.dao.*;
import com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.entity.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Service
public class TaxBillService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaxBillService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private TaxBillDao taxBillDao;
    @Resource
    private TaxBillSimpleDao taxBillSimpleDao;
    @Resource
    private TaxBillFileDao taxBillFileDao;
    @Resource
    private TaxBillCheckListFileDao taxBillCheckListFileDao;
    @Resource
    private TaxBillCatgDao taxBillCatgDao;
    @Resource
    private TaxBillSubDao taxBillSubDao;
    @Resource
    private TaxBillPaymentRecordDao taxBillPaymentRecordDao;
    @Resource
    private TaxBillPaymentDetailsDao taxBillPaymentDetailsDao;

    public void saveTaxBills(String taxBills) {
        if (StringUtils.isEmpty(taxBills)) return;
        String taxBillsText = SecurityUtil.decrypt3DES(token, taxBills);
        try {
            JSONObject taxBillObj = JSON.parseObject(taxBillsText);
            if (taxBillObj == null) return;
            String socialCreditCode = taxBillObj.getString("socialCreditCode");
            String payType = taxBillObj.getString("payType");
            JSONArray taxBillArr = taxBillObj.getJSONArray("taxBills");
            if (CollectionUtils.isEmpty(taxBillArr)) return;

            //电子税单列表拆分并存储
            List<TaxBill> taxBillList = new ArrayList<>();
            List<TaxBillSimple> simpleList = new ArrayList<>();
            for (int i = 0; i < taxBillArr.size(); i++) {
                JSONObject item = taxBillArr.getJSONObject(i);
                TaxBill taxBill = new TaxBill(socialCreditCode, payType, item);
                TaxBillSimple simple = taxBill.buildSimple();
                taxBillList.add(taxBill);
                simpleList.add(simple);
            }
            if (CollectionUtils.isEmpty(taxBillList)) return;

            this.deleteByTaxHeadSeqNo(simpleList.stream().map(TaxBillSimple::getTaxHeadSeqNo).collect(Collectors.toList()));

            taxBillDao.save(taxBillList);
            taxBillSimpleDao.save(simpleList);
        } catch (Exception e) {
            LOGGER.error("保存税费单错误", e);
        }
    }

    private void deleteByTaxHeadSeqNo(List<String> taxHeadSeqNos) {
        taxBillDao.deleteBy(taxHeadSeqNos);
        taxBillSimpleDao.deleteBy(taxHeadSeqNos);
    }

    public String taxBillFile(String swTaxId) {
        return taxBillFileDao.findBySwTaxId(swTaxId);
    }

    public void saveTaxBillFile(String swTaxId, String storedFile) {
        TaxBillFile taxBillFile = new TaxBillFile(swTaxId, storedFile);
        taxBillFileDao.save(taxBillFile);
    }

    public boolean taxBillFileStored(String swTaxId) {
        return taxBillFileDao.exists(swTaxId);
    }

    public String checkListFile(String taxHeadSeqNo) {
        return taxBillCheckListFileDao.findByTaxHeadSeqNo(taxHeadSeqNo);
    }

    public void saveCheckListFile(String taxHeadSeqNo, String storedFile) {
        TaxBillCheckListFile checkListFile = new TaxBillCheckListFile(taxHeadSeqNo, storedFile);
        taxBillCheckListFileDao.save(checkListFile);
    }

    public boolean checkListFileStored(String taxHeadSeqNo) {
        return taxBillCheckListFileDao.exists(taxHeadSeqNo);
    }

    public void saveTaxCatg(String swTaxId, String taxCatg) {
        if (StringUtils.isEmpty(taxCatg)) return;
        String taxCatgText = SecurityUtil.decrypt3DES(token, taxCatg);
        try {
            if (taxCatgText == null) return;
            TaxBillCatg catg = new TaxBillCatg(swTaxId, taxCatgText);

            taxBillCatgDao.delete(swTaxId);
            taxBillCatgDao.save(catg);
        } catch (Exception e) {
            LOGGER.error("保存税费单税种错误", e);
        }
    }

    public void saveTaxSub(String swTaxId, String taxCatgId, String taxSub) {
        if (StringUtils.isEmpty(taxSub)) return;
        String taxSubText = SecurityUtil.decrypt3DES(token, taxSub);
        try {
            if (taxSubText == null) return;
            TaxBillSub sub = new TaxBillSub(swTaxId, taxCatgId, taxSubText);

            taxBillSubDao.delete(swTaxId);
            taxBillSubDao.save(sub);
        } catch (Exception e) {
            LOGGER.error("保存税费单税目错误", e);
        }
    }

    public void savePaymentRecord(String swTaxId, String paymentRecord) {
        if (StringUtils.isEmpty(paymentRecord)) return;
        String paymentRecordText = SecurityUtil.decrypt3DES(token, paymentRecord);
        try {
            if (paymentRecordText == null) return;
            TaxBillPaymentRecord record = new TaxBillPaymentRecord(swTaxId, paymentRecordText);

            taxBillPaymentRecordDao.delete(swTaxId);
            taxBillPaymentRecordDao.save(record);
        } catch (Exception e) {
            LOGGER.error("保存税费单支付记录错误", e);
        }
    }

    public void savePaymentDetails(String taxPayId, String paymentDetails) {
        if (StringUtils.isEmpty(paymentDetails)) return;
        String paymentDetailsText = SecurityUtil.decrypt3DES(token, paymentDetails);
        try {
            if (paymentDetailsText == null) return;
            TaxBillPaymentDetails details = new TaxBillPaymentDetails(taxPayId, paymentDetailsText);

            taxBillPaymentDetailsDao.delete(taxPayId);
            taxBillPaymentDetailsDao.save(details);
        } catch (Exception e) {
            LOGGER.error("保存税费单支付明细错误", e);
        }
    }
}
